// pages/mycustom/mycustom.js
const app = getApp()
var CONFIG = require("../../config/config.js");
var share = require("../../utils/share.js");
// import "../public/sharetab.js";
Page({
    data: {
        teamtype: 1, //团队里面进入
        nolismsg: '',
        list: [],
        currentindex: -1,
        code: 0,
        msg: '',
        page: 0, //当前页数 默认0
        nowPage: 0, //起始条数 默认0
        count: 10, //所取条数 默认10
        keyword: '',
        status: 3, //客户状态（1潜在客户 2合作中 3全部）
        is_certif: 0,
        fromindex: 0,
        start_date: '', //开始时间
        end_date: '', //结束时间
        listfalse: false,
        closefalse: false,
        viewstatus: '', //客户状态（1潜在客户 2合作中）
        viewcertif: '',
        viewfromtext: '',
        viewstart_date: '', //开始时间
        viewend_date: '', //结束时间 
        showfalse: false,
        showfalse1: false,
        top: 0,
        firstfalse: true,
        secfalse: false,
        threefalse: false,
        identity: 1,
        navarrobj: {},
        tanfalse: false,
        adduser: 2,
        conCertif: 0,
        consultant_id: '',
    },


    onPageScroll: function(e) { //监听页面滚动
        // console.log(e);
        let that = this;
        var top = e.scrollTop;
        that.setData({
            top: top
        })
    },



    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        // console.log(89898)
        var that = this;
        if (options.teamtype) {
            if (Number(options.teamtype) == 2){
                var consultant_id = options.consultant_id;
            }else{
                var consultant_id = app.globalData.admin_id;
            }
            that.setData({
                teamtype: Number(options.teamtype),
                consultant_id: consultant_id
            })
        } else {
            var consultant_id = app.globalData.admin_id;
            that.setData({
                consultant_id: app.globalData.admin_id
            })
        }
        var navarr = JSON.parse(wx.getStorageSync('navarr'));
        var item = '';
        for (var i = 0; i < navarr.length; i++) {
            if (navarr[i].name == 'crm') {
                item = navarr[i];
            }
        }
        var obj = {
            navarr: navarr,
            typestatus: 'crm',
        }
        var adduser = 2;
        if (item.children && Number(item.children.length) > 0) {
            for (var i = 0; i < item.children.length; i++) {
                if (item.children[i].name == 'adduser') {
                    adduser = 1;
                }
            }
        }
        that.setData({
            navarrobj: obj,
            tanfalse: true,
            adduser: 1,
        })
    },





    onShow(options) {
        wx.showLoading({
            title: '加载中~',
            mask: true
        })
        var that = this;
        var identity = app.globalData.identity;
        that.setData({
            identity: identity
        })
        if (wx.getStorageSync('phoneseacher')) {
            wx.removeStorageSync('phoneseacher');
        }
        // 获取课程顾问id
        var consultant_id = that.data.consultant_id;
        var thatdata = that.data;
        //   console.log(wx.getStorageSync('keywordphone'))

        // wx.setStorageSync('keymycoust', 1);

        if (wx.getStorageSync('keymycoust')) {
            var keymycoust = wx.getStorageSync('keymycoust');
            if (keymycoust == 1 || keymycoust == '1') {
                wx.removeStorageSync('keymycoust');
            } else {
                if (wx.getStorageSync('keywordphone')) {
                    // console.log(8989);
                    that.setData({
                        keyword: wx.getStorageSync('keywordphone')
                    })
                }
            }
        } else {
            if (wx.getStorageSync('keywordphone')) {
                // console.log(8989);
                that.setData({
                    keyword: wx.getStorageSync('keywordphone')
                })
            }
        }

        that.setData({
            list: [],
            page: 0, //当前页数 默认0
            nowPage: 0, //起始条数 默认0
            count: 10, //所取条数 默认10
        })
        // 获取筛选数据
        setTimeout(function() {
            that.setData({
                showfalse: true
            })
            that.parmadata(consultant_id);
        }, 500)

        // 获取筛选内容
        if (wx.getStorageSync('prma')) {
            var parmdata = JSON.parse(wx.getStorageSync('prma'));
            that.setData({
                closefalse: true,
                viewstatus: parmdata.status, //客户状态
                viewfromtext: parmdata.fromindex,
            })

            if (parmdata.start_date != '' && parmdata.end_date != '') {
                that.setData({
                    viewstart_date: parmdata.start_date, //开始时间
                    viewend_date: parmdata.end_date, //结束时间 
                })
            }
        }
    },

    // 获取筛选数据 
    parmadata: function(consultant_id) {
        var that = this;
        var thatdata = that.data;
        if (wx.getStorageSync('parmdata')) {
            var parmdata = JSON.parse(wx.getStorageSync('parmdata'));
            that.setData({
                status: parmdata.status, //客户状态（1潜在客户 2合作中）
                fromindex: parmdata.fromindex,
            })
            if (parmdata.start_date != '' && parmdata.end_date != '') {
                that.setData({
                    start_date: parmdata.start_date, //开始时间
                    end_date: parmdata.end_date, //结束时间
                })
                var start_date = parmdata.start_date;
                var end_date = parmdata.end_date;
                var fromindex = parmdata.fromindex;
                var status = parmdata.status;
                var keyword = '';
            } else {
                var start_date = '';
                var end_date = '';
                var fromindex = parmdata.fromindex;
                var status = parmdata.status;
                var keyword = '';
            }
        } else {
            var status = 3;
            var keyword = thatdata.keyword;
            var start_date = '';
            var end_date = '';
            var fromindex = '';
            
        }
        var data = {
            status: status, //客户状态（1潜在客户 2合作中）
            passive: fromindex,
            start_date: start_date, //开始时间
            end_date: end_date, //结束时间
            keyword: keyword,
        }
        if (Number(that.data.teamtype)==3){
            data['phone'] = wx.getStorageSync('tel')
        }else{
            data['consultant_id'] = consultant_id;
        }
        that.getMyStudentsInit(data);
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {
        if (wx.getStorageSync('keywordphone')) {
            wx.removeStorageSync('keywordphone')
        }
        var that = this;
        var consultant_id = that.data.consultant_id;
        that.setData({
            closefalse: false,
            list: [],
            status: 3, //客户状态（1潜在客户 2合作中）
            start_date: '', //开始时间
            end_date: '', //结束时间
            page: 0, //当前页数 默认0
            nowPage: 0, //起始条数 默认0
            count: 10, //所取条数 默认10
            viewstatus: '', //客户状态（1潜在客户 2合作中）
            viewstart_date: '', //开始时间
            viewend_date: '', //结束时间 
            viewfromtext: '',
            fromindex: 0,
            keyword: '',
        })
        var data = {
            consultant_id: consultant_id, //课程顾问ID
            status: 3, //客户状态（1潜在客户 2合作中）
        }
        if (Number(that.data.teamtype) == 3) {
            var data = {
                phone: wx.getStorageSync('tel'), //课程顾问ID
                status: 3, //客户状态（1潜在客户 2合作中）
            }
        } else {
            var data = {
                consultant_id: consultant_id, //课程顾问ID
                status: 3, //客户状态（1潜在客户 2合作中）
            }
        }
        that.getMyStudentsInit(data);
        wx.stopPullDownRefresh();
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {
        var that = this;
        var consultant_id = that.data.consultant_id;
        // var nowPage = ++page;  //当前页（默认第一页）
        // var count = that.data.count + 10;
        that.setData({
            page: that.data.page + 1,
            nowPage: that.data.page + 1,
            count: 10,
        })
        var parmdata = that.data;
        if (parmdata.start_date != '' && parmdata.end_date != '') {
            var data = {
                status: parmdata.status, //客户状态（1潜在客户 2合作中）
                passive: parmdata.fromindex,
                start_date: parmdata.start_date, //开始时间
                end_date: parmdata.end_date, //结束时间
                keyword: parmdata.keyword,
                nowPage: parmdata.nowPage,
                count: parmdata.count,
            }
            if (Number(that.data.teamtype) == 3) {
                data['phone'] = wx.getStorageSync('tel');
            } else {
                data['consultant_id'] = consultant_id;
            }
        } else {
            var data = {
                status: parmdata.status, //客户状态（1潜在客户 2合作中）
                passive: parmdata.fromindex,
                keyword: parmdata.keyword,
                nowPage: parmdata.nowPage,
                count: parmdata.count,
            }
            if (Number(that.data.teamtype) == 3) {
                data['phone'] = wx.getStorageSync('tel');
            } else {
                data['consultant_id'] = consultant_id;
            }
        }
        that.getMyStudentsInit(data);
    },




    // 输入框值改变时
    searchStudent: function(e) {
        // console.log(e);
        var keyword = e.detail.value;
        wx.setStorageSync('keywordphone', keyword);
        var that = this;
        that.setData({
            list: [],
            keyword: e.detail.value,
            coursefalse: false,
            statefalse: false,
            page: 0, //当前页数 默认0
            nowPage: 0, //起始条数 默认0
            count: 10, //所取条数 默认10
        })

        var consultant_id = that.data.consultant_id;
        var parmdata = that.data;
        if (parmdata.start_date != '' && parmdata.end_date != '') {
            var data = {
                status: parmdata.status, //客户状态（1潜在客户 2合作中）
                start_date: parmdata.start_date, //开始时间
                end_date: parmdata.end_date, //结束时间
                keyword: e.detail.value,
                passive: parmdata.fromindex,
            }
            if (Number(that.data.teamtype) == 3) {
                data['phone'] = wx.getStorageSync('tel');
            } else {
                data['consultant_id'] = consultant_id;
            }
        } else {
            var data = {
                status: parmdata.status, //客户状态（1潜在客户 2合作中）
                keyword: e.detail.value,
                passive: parmdata.fromindex,
            }
            if (Number(that.data.teamtype) == 3) {
                data['phone'] = wx.getStorageSync('tel');
            } else {
                data['consultant_id'] = consultant_id;
            }
        }
        //   console.log(data);
        that.getMyStudentsInit(data);
    },

    // 搜索内容
    getMyStudentsInit: function(data) {
        wx.showLoading({
            title: '加载中~',
            mask: true
        })
        let that = this;
        if (Number(that.data.teamtype) == 3) {
            var url = 'crmapi/Teams/teamsUserList';
        } else {
            if (Number(app.globalData.identity)==3){
                var url = 'crmapi/Learn/userList';
            }else{
                var url = 'crmapi/user/userList';
            }
        }
        share.wxRequest(url, data, function(res) {
            // console.log('接口'+res);
            // console.log(res);
            wx.hideLoading();
            if (Number(res.data.errcode) == 0) {
                var reslist = res.data.data;
                // console.log(that.data.page);
                if (that.data.page == 0) {
                    that.setData({
                        list: [],
                        conCertif: Number(res.data.conCertif),
                    })
                    var thatlist = that.data.list;
                } else {
                    that.setData({
                        list: that.data.list
                    })
                    var thatlist = that.data.list;
                }
                var reslists = that.data.list.concat(reslist);

                // console.log(that.data.list);
                // console.log(thatlist);

                if (reslist >= 10) {
                    that.setData({
                        list: reslists,
                        msg: res.data.msg,
                        code: 0,
                        listfalse: false
                    })
                } else {
                    that.setData({
                        list: reslists,
                        msg: '——已经到底啦——',
                        code: 2,
                    })
                    if (thatlist.length != 0) {
                        that.setData({
                            listfalse: true
                        })
                    } else {
                        that.setData({
                            listfalse: false
                        })
                    }
                }
                if (Number(that.data.list.length) == 0) {
                    that.setData({
                        nolismsg: '暂无数据'
                    })
                }
            } else {
                that.setData({
                    nolismsg: res.data.msg
                })
                // wx.showModal({
                //   title: CONFIG.MODEL_TITLE,
                //   showCancel: false,
                //   content: CONFIG.REQUEST_FAIL,
                //   confirmText: '点击重试',
                //   success: function (res) {
                //     wx.navigateBack();
                //   }
                // })
            }
        })
    },

    // 用户点击右上角分享
    onShareAppMessage: function() {
        return {
            title: CONFIG.SHARETITLE,
            path: CONFIG.PATHLOGIN
        }
    },

    // 点击关闭
    closeclick: function() {
        var that = this;
        var consultant_id = that.data.consultant_id;
        that.hide();
        that.setData({
            closefalse: false
        })
        that.setData({
            list: [],
            status: 3, //客户状态（1潜在客户 2合作中）
            fromindex: 0,
            start_date: '', //开始时间
            end_date: '', //结束时间
            page: 0, //当前页数 默认0
            nowPage: 0, //起始条数 默认0
            count: 10, //所取条数 默认10
            keyword: '',
        })
        var data = {
            status: 3, //客户状态（1潜在客户 2合作中）
        }
        if (Number(that.data.teamtype) == 3) {
            data['phone'] = wx.getStorageSync('tel');
        } else {
            data['consultant_id'] = consultant_id;
        }
        setTimeout(function() {
            that.getMyStudentsInit(data);
        }, 300)
    },

    onHide() {
        this.hide();
        this.setData({
            closefalse: false,
            showfalse: false,
            keyword: '',
        })
    },
    onUnload() {
        this.hide();
        this.setData({
            closefalse: false,
            showfalse: false,
            keyword: '',
        })
    },


    hide: function() {
        var that = this;
        wx.removeStorageSync('parmdata')
        wx.removeStorageSync('prma')
        that.setData({
            status: 3, //客户状态（1潜在客户 2合作中）
            is_certif: 0,
            fromindex: 0,
            start_date: '', //开始时间
            end_date: '', //结束时间
            page: 0, //当前页数 默认0
            nowPage: 0, //起始条数 默认0
            count: 10, //所取条数 默认10
            viewstatus: '', //客户状态（1潜在客户 2合作中）
            viewfromtext: '',
            viewcertif: '',
            viewstart_date: '', //开始时间
            viewend_date: '', //结束时间 
        })
    }
})